﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HotelMgr.Models;
using System.Data;
using System.Data.Entity;
using HotelMgr.ViewModels;

namespace HotelMgr.Controllers
{
    public class RoomSearchController : Controller
    {
        private HotelEntities db = new HotelEntities();
        //
        // GET: /RoomSearch/

        public ActionResult Index()
        {
            var roomTypes = db.RoomTypes;
            var roomStates = db.RoomStates;
            var rooms = db.Rooms.Include(r => r.RoomType).Include(r => r.RoomState);
            var vmSearchRoom = new VMOrderRoomList()
                                   {
                                       RoomTypes = roomTypes.ToList(),
                                       RoomStates = roomStates.ToList(),
                                       Rooms = rooms.ToList()
                                   };
            return View(vmSearchRoom);
        }
        [HttpPost]
        public ActionResult Index(List<int> roomTypeFilter,List<int> roomStateFilter, int? roomNum_lower, int? roomNum_upper, decimal? price_lower, decimal? price_upper)
        {
            var rooms = db.Rooms.Include(r => r.RoomType).Include(r => r.RoomState);
            rooms = rooms.Where(r => roomTypeFilter.Contains(r.RoomTypeID) && roomStateFilter.Contains(r.RoomStateID));
            if (roomNum_lower.HasValue)
            {
                rooms = rooms.Where(r => r.RoomID >= roomNum_lower.Value);
            }
            if (roomNum_upper.HasValue)
            {
                rooms = rooms.Where(r => r.RoomID <= roomNum_upper.Value);
            }
            if (price_lower.HasValue)
            {
                rooms = rooms.Where(r => r.RoomPrice >= price_lower);
            }
            if (price_upper.HasValue)
            {
                rooms = rooms.Where(r => r.RoomPrice <= price_upper);
            }
            return PartialView("RoomList", rooms.ToList());
        }
    }
}
